package org.redr.canvas;

import java.util.List;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsItemInterface;
import com.trolltech.qt.gui.QGraphicsLineItem;
import com.trolltech.qt.gui.QGraphicsRectItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QLineF;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPen;

public class TempCanvasLine extends QGraphicsLineItem {
	private RRWIcon startWidget = null;
	private RRWIcon endWidget = null;
	private RRWIcon widget = null;
	
	
	public TempCanvasLine(QGraphicsScene qGraphicsScene) {
		super(null, qGraphicsScene);
		
		setZValue(-10);
		setPen(new QPen(new QColor(RedRApplication.coreSettings().getSetting("lineColor")), 1, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap));
		// setLine(new QLineF());
	}
	
	public void setStartWidget(RRWIcon icon){
		startWidget = icon;
		QPointF pos = icon.getRightEdgePoint();
		System.out.println(String.format("I think the line starts at pos x: %s, y: %s", pos.x(), pos.y()));
		setLine((int) pos.x(), (int) pos.y(), (int) pos.x(), (int) pos.y());
	}
	
	public void setEndWidget(RRWIcon icon) {
		endWidget = icon;
		QPointF pos = icon.getLeftEdgePoint();
		System.out.println(String.format("I think the line starts at pos x: %s, y: %s", pos.x(), pos.y()));
		setLine((int) pos.x(), (int) pos.y(), (int) pos.x(), (int) pos.y());
	}

	public void updateLinePos(QPointF point) {
		if ((this.startWidget == null) & (this.endWidget == null)){
			return;
		}
		this.widget = null;
		
		// set the current anchor pos
		QPointF pos;
		
		if (this.startWidget != null) pos = this.startWidget.getRightEdgePoint();
		else pos = this.endWidget.getLeftEdgePoint();
		
		SchemaView view = RedRObjects.getActiveTab();
		this.widget = view.getIconAtPos(point);
		
		if ((this.widget != null) & (this.widget != this.startWidget) & (this.widget != this.endWidget)){
			if ((startWidget != null) & (widget.instance().outputs().size() > 0)) point = widget.getLeftEdgePoint();
			else if ((endWidget != null) & (widget.instance().inputs().size() > 0)) point = widget.getRightEdgePoint();
		}
		this.setLine(pos.x(), pos.y(), point.x(), point.y());
	}
	
	public void drawShape(QPainter painter){
		QLineF line = line();
		System.out.println("Calling draw shape.");
		if (line == null) {
			System.out.println("Line appears to be null");
			return;
		}
		double startX = line.p1().x();
		double startY = line.p1().y();
		double endX = line.p2().x();
		double endY = line.p2().y();
		
		painter.setPen(new QPen(new QColor(RedRApplication.coreSettings().getSetting("lineColor")), 1, Qt.PenStyle.SolidLine));
		// setLine(new QLineF(new QPointF(startX, startY), new QPointF(endX, endY)));
		painter.drawLine(new QPointF(startX, startY), new QPointF(endX, endY));
	}

	public RRWIcon startWidget() {
		// TODO Auto-generated method stub
		return this.startWidget;
	}

	public RRWIcon widget() {
		// TODO Auto-generated method stub
		return this.widget;
	}

	public RRWIcon endWidget() {
		// TODO Auto-generated method stub
		return this.endWidget;
	}
	
	public void remove(){
		hide();
	}
}
